package com.wzq.solution;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

class Solution17 {
    public static List<String> letterCombinations(String digits) {
        Map<Character,Character[]> m = new HashMap<Character,Character[]>();

        m.put('2',new Character[]{'a','b','c'});
        m.put('3',new Character[]{'d','e','f'});
        m.put('4',new Character[]{'g','h','i'});
        m.put('5',new Character[]{'j','k','l'});
        m.put('6',new Character[]{'m','n','o'});
        m.put('7',new Character[]{'p','q','r','s'});
        m.put('8',new Character[]{'t','u','v'});
        m.put('9',new Character[]{'w','x','y','z'});

        List<StringBuilder> lsb = new ArrayList<StringBuilder>();

        for(int i=0;i<digits.length();i++){
            Character[] list = m.get(digits.charAt(i));
            int ll = lsb.size();
            if(ll == 0){
                for(int j=0;j<list.length;j++){
                    char c = list[j];
                    StringBuilder tsb = new StringBuilder();
                    tsb.append(c);
                    lsb.add(tsb);
                }
            }else{
                int newLL = list.length*ll;
                for(int j=newLL-1;j>=0;j--){
                    StringBuilder tsb = new StringBuilder();
                    tsb.append(lsb.get(j%ll).toString()).append(list[j/ll]);
                    if(j>=ll){
                        lsb.add(tsb);
                    }else{
                        lsb.set(j,tsb);
                    }

                }
            }

        }


        List<String> rst = new ArrayList();
        for(StringBuilder sb:lsb){
            rst.add(sb.toString());
        }
        return rst;
    }
    public static void main(String[] args) {
        letterCombinations("23");
    }
}